﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class EqualToValidatorAttribute : BaseValidatorAttribute
    {
        public EqualToValidatorAttribute(string otherName) : this(otherName, ValidationUtils.Messages.EqualTo)
        {
        }

        public EqualToValidatorAttribute(string otherName, string errorMessage) : base(errorMessage)
        {
            OtherName = otherName;
        }

        public string OtherName { get; protected set; }


        public override bool IsValid(object value, ValidationContext validationContext)
        {
            var item = validationContext.GetValidationItem(OtherName);
            if (item == null)
            {
                return false;
            }
            else
            {
                if (item.Value == null)
                {
                    return value == null;
                }
                return item.Value.Equals(value);
            }
        }
    }
}
